package com.jjjjbababy.custom_view

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat


/**
 * Created by helu on 2022/4/15.
 * Description:仿即刻点赞数字动画
 */
class MagicNumberTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val textList = mutableListOf<Int>()
    private var lastTextList = mutableListOf<Int>()

    private val paint by lazy {
        Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = ContextCompat.getColor(context, R.color.teal_200)
            textSize = DisplayUtil.dp2px(context, 25f).toFloat()
        }
    }

    private var fraction = 0f
    set(value) {
        field = value
        invalidate()
    }

    private val animator = ObjectAnimator.ofFloat(this,"fraction",0f,1f)
    var number = 0
    set(value) {
        field = value
        lastTextList.clear()
        val lastCharArray = value.toString().toCharArray()
        for (c in lastCharArray){
            lastTextList.add(Character.getNumericValue(c))
        }
        invalidate()
    }
    private var isLike = false

    fun likeOrNot(){
        if (animator.isRunning) return
        val lastNumber = number
        isLike = !isLike
        if(isLike) ++number else --number
        assert(number>0){ throw IllegalStateException("number can not be smaller than zero") }
        val lastCharArray = lastNumber.toString().toCharArray()
        lastTextList.clear()
        for (c in lastCharArray){
            lastTextList.add(Character.getNumericValue(c))
        }
        val charArray = number.toString().toCharArray()
        textList.clear()
        for (c in charArray) {
            textList.add(Character.getNumericValue(c))
        }
        animator.start()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension((paint.measureText("0") * 6).toInt(), (paint.fontMetrics.bottom - paint.fontMetrics.top).toInt())
        } else if (widthMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(widthSize,(paint.fontMetrics.bottom - paint.fontMetrics.top).toInt())
        } else if(heightMode == MeasureSpec.EXACTLY){
            setMeasuredDimension((paint.measureText("0") * 6).toInt(),heightSize)
        }
    }

    override fun onDraw(canvas: Canvas) {
        paint.color = if(isLike) ContextCompat.getColor(context,R.color.teal_200) else ContextCompat.getColor(context,R.color.black)
        val charWidth = paint.measureText("0")
            if (lastTextList.size == textList.size) {
                for ((index, item) in lastTextList.withIndex()) {
                    if (item == textList[index]) {
                        paint.alpha = 255
                        canvas.drawText(item.toString(),charWidth*index,height/2-(paint.fontMetrics.top + paint.fontMetrics.bottom)/2,paint)
                        continue
                    }else{
                        paint.alpha = (255-255*fraction).toInt()
                        canvas.drawText(item.toString(),charWidth*index,height/2-(paint.fontMetrics.top + paint.fontMetrics.bottom)/2 - height * fraction,paint)
                        paint.alpha = 255*fraction.toInt()
                        canvas.drawText(textList[index].toString(),charWidth*index,3 * height/2-(paint.fontMetrics.top + paint.fontMetrics.bottom)/2 - height * fraction,paint)
                    }
                }
            }else{
                paint.alpha = (255-255*fraction).toInt()
                for ((index, item) in lastTextList.withIndex()){
                    canvas.drawText(item.toString(),charWidth*index,height/2-(paint.fontMetrics.top + paint.fontMetrics.bottom)/2 - height * fraction,paint)
                }
                paint.alpha = 255*fraction.toInt()
                for ((index, item) in textList.withIndex()){
                    canvas.drawText(item.toString(),charWidth*index,3 * height/2-(paint.fontMetrics.top + paint.fontMetrics.bottom)/2 - height * fraction,paint)
                }
            }
    }
}